package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.linlinjava.litemall.db.dao.LitemallUserTokenMapper;
import org.linlinjava.litemall.db.domain.LitemallUserToken;
import org.linlinjava.litemall.db.domain.LitemallUserTokenExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LitemallUserTokenService {
	@Resource
	private LitemallUserTokenMapper tokenMapper;

	private UserTokenService usertokenMapper;

	public LitemallUserToken findByUid(Integer uid) {
		LitemallUserTokenExample example = new LitemallUserTokenExample();
		example.or().andUserIdEqualTo(uid);
		return tokenMapper.selectOneByExample(example);
	}

	public List<LitemallUserToken> queryAll() {
		LitemallUserTokenExample example = new LitemallUserTokenExample();
		return tokenMapper.selectByExample(example);
	}

	public List<LitemallUserToken> queryByUserIds(List<Integer> userIds) {
		LitemallUserTokenExample example = new LitemallUserTokenExample();
		example.or().andUserIdIn(userIds);
		return tokenMapper.selectByExample(example);
	}

	public void deleteById(Integer id) {
		tokenMapper.deleteByPrimaryKey(id);
	}

	public void add(LitemallUserToken userToken) {
		userToken.setAddTime(LocalDateTime.now());
		userToken.setUpdateTime(LocalDateTime.now());
		tokenMapper.insertSelective(userToken);
	}

	public int update(LitemallUserToken userToken) {
		userToken.setUpdateTime(LocalDateTime.now());
		return tokenMapper.updateByPrimaryKeySelective(userToken);
	}

	public void updateByUserIds(List<Integer> userIds) {
		LitemallUserTokenExample example = new LitemallUserTokenExample();
		example.or().andUserIdIn(userIds).andStatusEqualTo(true);
		LitemallUserToken userToken = new LitemallUserToken();
		userToken.setStatus(false);
		tokenMapper.updateByExampleSelective(userToken, example);
		
		usertokenMapper.updateUserTokenCatch(userIds);
	}
}
